{{Extend "db/index"}}
{{Block "title"}}{{"数据库"|T}}: {{Stored.dbName}} - {{Super}}{{/Block}}
{{Block "breadcrumb"}}
{{Super}}
<li class="active">{{"数据库"|T}}: {{Stored.dbName}}</li>
{{/Block}}
{{Block "main"}}
<div class="row">
    <div class="col-md-12">
        <div class="block-flat">
          <div class="header">
			<a href="{{dbMgrURL "createTable" Stored.dbName}}" class="btn btn-success pull-right">
				<i class="fa fa-plus"></i>
				{{"创建表"|T}}
			</a>						
            <h3>{{"数据表"|T}}</h3>
          </div>
          <div class="content">
             <div class="table-responsive">
				<table class="table no-border hover blue">
					<thead class="no-border">
						<tr>
							<th style="width:2%;"><strong></strong></th>
							<th><strong>{{"表"|T}}</strong></th>
							<th><strong>{{"引擎"|T}}</strong></th>
							<th><strong>{{"字符集"|T}}</strong></th>
							<th><strong>{{"数据长度"|T}}</strong></th>
							<th><strong>{{"索引长度"|T}}</strong></th>
							<th><strong>{{"数据空闲"|T}}</strong></th>
							<th><strong>{{"自动增量"|T}}</strong></th>
							<th><strong>{{"行数"|T}}</strong></th>
							<th><strong>{{"注释"|T}}</strong></th>
						</tr>
					</thead>
					<tbody class="no-border-x" id="tbody-content">
                        {{$dbName := Stored.dbName}}
                        {{$tableStatus := Stored.tableStatus}}
                        {{range $k,$v := Stored.tableList}}
						{{$info := index $tableStatus $v}}
						<tr>
                            <td>
                                <input id="checkbox-{{$k}}" class="icheck" type="checkbox" name="{{if $info.IsView}}view{{else}}table{{end}}[]" value="{{$v}}">
                            </td>
							<td><a href="{{dbMgrURL "viewTable" $dbName $v}}">{{$v}}</a></td>
							<td>{{$info.Engine.String}}</td>
							<td>{{$info.Collation.String}}</td>
							<td>{{$info.Data_length.Int64}}</td>
                            <td>{{$info.Index_length.Int64}}</td>
							<td>{{$info.Data_free.Int64}}</td>
							<td>{{$info.Auto_increment.Int64}}</td>
							<td>{{$info.Rows.Int64}}</td>
							<td>{{$info.Comment.String}}</td>
						</tr>
                        {{end}}
					</tbody>
				</table>		
			    </div>
                <fieldset>
                     <legend>{{"已选中"|T}} (<span id="selected-items-count">0</span>)</legend>
                     <button type="button" class="btn btn-default" id="analyze-selected">{{"分析"|T}}</button>
                     <button type="button" class="btn btn-default" id="optimize-selected">{{"优化"|T}}</button>
                     <button type="button" class="btn btn-default" id="check-selected">{{"检查"|T}}</button>
                     <button type="button" class="btn btn-default" id="repair-selected">{{"修复"|T}}</button>
                     <button type="button" class="btn btn-default" id="truncate-selected">{{"清空"|T}}</button>
                     <button type="button" class="btn btn-default" id="drop-selected">{{"删除"|T}}</button>
                     <button type="button" class="btn btn-default" id="move-selected">{{"移动"|T}}</button>
                     <button type="button" class="btn btn-default" id="copy-selected">{{"复制"|T}}</button>
                </fieldset>
          </div><!-- /.content -->
        </div><!-- /.block-flat -->
    </div>
</div>
{{/Block}}

{{Block "footer"}}
{{Super}}

{{Modal "__TMPL__/db/mysql/copy_table.yaml"}}
{{Modal "__TMPL__/db/mysql/move_table.yaml"}}
<script type="text/template" id="tpl-move-table-form">
    <div class="form-group">
        <label>{{"移动表"|T|ToJS}}</label>
        <textarea readonly="readonly" class="form-control" id="move-tables">
        </textarea>
    </div>
    <div class="form-group">
        <label>{{"到数据库"|T|ToJS}}</label>
        <select class="form-control" id="move-table-to-db">
			<option value="" disabled>loading...</option>
        </select>
    </div>
</script>
<script type="text/template" id="tpl-copy-table-form">
    <div class="form-group">
        <label>{{"复制表"|T|ToJS}}</label>
        <textarea readonly="readonly" class="form-control" id="copy-tables">
        </textarea>
    </div>
    <div class="form-group">
        <label>{{"到数据库"|T|ToJS}}</label>
        <select class="form-control" id="copy-table-to-db">
			<option value="" disabled>loading...</option>
        </select>
    </div>
</script>
<script>
var currentDb='{{Stored.dbName}}';
function refreshList(){
    $.get(window.location.href,{'_pjax':'#tbody-content'},function(r){
        var e=$(r);
        $('#tbody-content').html(e.find('#tbody-content').html());
        perttyCheckbox();
    },'html');
}
function getSelectedItems(){
    var tables=[],views=[];
    $('#tbody-content input[name="table[]"]:checked').each(function(){
        tables.push($(this).val());
    });
    $('#tbody-content input[name="view[]"]:checked').each(function(){
        views.push($(this).val());
    });
    return {tables:tables,views:views};
}
function perttyCheckbox(){
    $('#selected-items-count').text($('#tbody-content .icheck:checked').length);
    App.iCheck('#tbody-content .icheck','click',function(){
        var v=$(this).prop('checked')?1:-1;
        var c=$('#selected-items-count');
        var n=parseInt(c.text());
        c.text(n+v);
    });
}
$(function(){
    perttyCheckbox();

    $('#move-table-modal .modal-body').html($('#tpl-move-table-form').html());
    $('#copy-table-modal .modal-body').html($('#tpl-copy-table-form').html());
    $('#move-table-modal .btn-primary:last').removeClass('md-close').on('click',function(){
        var dbName=$('#move-table-to-db').val();
        var se=getSelectedItems();
        $.post(window.location.href,{json:'move',dbName:dbName,table:se.tables,view:se.views},function(r){
            if(r.Code==1){
                refreshList();
                $('#move-table-modal').niftyModal('hide');
                App.showDbLog(r.Data);
            }else{
                alert(r.Info);
            }
        },'json');
    });
    $('#copy-table-modal .btn-primary:last').removeClass('md-close').on('click',function(){
        var dbName=$('#copy-table-to-db').val();
        var se=getSelectedItems();
        $.post(window.location.href,{json:'copy',dbName:dbName,table:se.tables,view:se.views},function(r){
            if(r.Code==1){
                refreshList();
                $('#copy-table-modal').niftyModal('hide');
                App.showDbLog(r.Data);
            }else{
                alert(r.Info);
            }
        },'json');
    });

    $('#move-selected,#copy-selected').on('click',function(event){
		var op,tx;
		if($(this).attr('id')=='move-selected'){
			op='move';
			tx='{{"移动"|T}}';
		}else{
			op='copy';
			tx='{{"复制"|T}}';
		}
        var se=getSelectedItems();
        if(se.tables.length<1&&se.views.length<1){
            return App.message({title:'{{"出错了"|T}}',text:String('{{"请选择您要{op}的表"|T}}').replace(/\{op\}/,tx),class_name:'danger'},false);
        }
        var sed=se.tables.join(',');
        if(se.views.length>0){
            if(sed)sed+=',';
            sed+=se.views.join(',');
        }
		$('#'+op+'-tables').val(sed);
		$('#'+op+'-table-modal').niftyModal('show',{afterOpen:function(modal){
            if(!$('#'+op+'-table-to-db').data('loaded')){
                $('#'+op+'-table-to-db').data('loaded',1);
                $.get(window.location.href,{json:'dbs'},function(r){
                    if(r.Code<1){
                        return App.message(r.Info);
                    }
                    var str='';
                    for(var i=0;i<r.Data.length;i++){
						var s=r.Data[i]==currentDb?' selected':'';
                        str+='<option value="'+r.Data[i]+'"'+s+'>'+r.Data[i]+'</option>';
                    }
                    $('#'+op+'-table-to-db').html(str);
                },'json');
            }
        }});
	});
    $('#analyze-selected,#optimize-selected,#check-selected,#repair-selected,#truncate-selected,#drop-selected').on('click',function(event){
        var op,tx;
        switch($(this).attr('id')){
            case 'analyze-selected':
			op='analyze';
			tx='{{"分析"|T}}';
            break;
            case 'optimize-selected':
			op='optimize';
			tx='{{"优化"|T}}';
            break;
            case 'check-selected':
			op='check';
			tx='{{"检查"|T}}';
            break;
            case 'repair-selected':
			op='repair';
			tx='{{"修复"|T}}';
            break;
            case 'truncate-selected':
			op='truncate';
			tx='{{"清空"|T}}';
            break;
            case 'drop-selected':
			op='drop';
			tx='{{"删除"|T}}';
            break;
        }
        var se=getSelectedItems();
        if(se.tables.length<1&&se.views.length<1){
            return App.message({title:'{{"出错了"|T}}',text:String('{{"请选择您要{op}的表"|T}}').replace(/\{op\}/,tx),class_name:'danger'},false);
        }
        if(op=='truncate'||op=='drop'){
            var sed=se.tables.join('\n');
            if(se.views.length>0){
                if(sed)sed+='\n';
                sed+=se.views.join('\n');
            }
            if(!confirm(String('{{"确定要{op}下面这些表吗？"|T}}').replace(/\{op\}/,tx)+'\n'+sed)){
                return;
            }
        }
        $.post(window.location.href,{json:op,table:se.tables,view:se.views},function(r){
            var hasData=typeof(r.Data)!='undefined';
            if(r.Code==1||(hasData && r.Data && r.Data.length>1)){
                refreshList();
                if(hasData&&r.Data){
                    for(var i=0;i<r.Data.length;i++){
                        App.showDbLog(r.Data[i]);
                    }
                }
            }else{
                App.message({title:'{{"出错了"|T}}',text:r.Info,class_name:'danger'},false);
            }
        },'json');
    });
});
</script>
{{/Block}}